import React from 'react';
import {
    ActivityIndicator,
    Button,
    Text,
    View,
    Image,
    StyleSheet,
    Dimensions,
    StatusBar,
    FlatList,
    ScrollView,
    RefreshControl,
    Animated,
    TouchableHighlight,
    SectionList, TouchableOpacity, DeviceEventEmitter
} from 'react-native';

//引入
import ScrollableTabView, { DefaultTabBar, ScrollableTabBar } from 'react-native-scrollable-tab-view';
import Styles from './Styles';
import app from '../../../app.json';
import Ugame from '../../../Ugame';
import Httpc from "../../utils/Httpc";
import REQUEST from "../../../REQUEST";
import Football from "../../utils/Football";
import Utils from "../../utils/Utils";
const { width, height } = Dimensions.get('window')

export default class ListsDetails extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            page: 0,
            rows: 10,
            total: 0,
            totalpage: 1,
            lists: [],
            loaded: true,
            refreshing: false,
        };
        this.type = 0;
        this.label = ['全部', '充值', '投注', '派奖', '后台操作', '撤销投注', '提现'];
    }
    static navigationOptions = {
        //title: '彩票咨询',
        //header: null,  //隐藏顶部导航栏
        //tabBarVisible: false, // 隐藏底部导航栏
        headerStyle: {
            backgroundColor: '#d0162e',
            paddingTop: 18,
        },
        headerTintColor: '#fff',
        headerTitleStyle: {
            //fontWeight: 'bold',
        },
    }
    componentWillMount() {
        this.type = this.label.indexOf(this.props.tabLabel);
    }
    componentDidMount() {
        
        this.getServerData(() => { });
    }
    getServerData(callback) {
        let _this = this;
        let params = {};
        let page = this.state.page;
        let rows = this.state.rows;
        let lists = this.state.lists;
        params.rootid = app.rootid;
        params.token = Ugame.info.Token;
        params.type = this.type;
        params.page = page + 1;
        params.rows = rows;
        Httpc.get(REQUEST.GetUserDetail, params, (res) => {
            if (res.StatusCode == -2) {
                Ugame.clearInfo();
                this.props.navigation.navigate('Login');
                return;
            }
            if (res.StatusCode == 1) {
                _this.setState({
                    page: page + 1,
                    total: res.Data.total,
                    totalpage: res.Data.totalpage,
                    lists: lists.concat(res.Data.rows),
                });
            }
            callback();
        })
    }
    _createNewsItem = ({ item }) => {
        return (
            <View style={{ height: 60, padding: 12, flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }} >
                <View style={{ flexDirection: 'column', justifyContent: 'center', alignItems: 'flex-start' }}>
                    <Text>{item.ud_Note}</Text>
                    <Text style={{ fontSize: 12, color: '#666' }}>{item.ud_CTime.substr(0, 19)}</Text>
                </View>
                {item.ud_NowCoin >= 0 ?
                    <Text style={{ fontSize: 12, color: '#d0162e' }}>+{item.ud_NowCoin}元</Text> :
                    <Text style={{ fontSize: 12, color: '#333333' }}>{item.ud_NowCoin}元</Text>
                }
            </View>
        );
    }
    _onRefresh = () => {
        if (this.state.refreshing) {
            return;
        }
        this.setState({ refreshing: true });
        let _this = this;
        let params = {};
        params.rootid = app.rootid;
        params.token = Ugame.info.Token;
        params.type = this.type;
        params.page = 1;
        params.rows = this.state.rows;
        Httpc.get(REQUEST.GetUserDetail, params, function (res) {
            _this.setState({ refreshing: false });
            if (res.StatusCode == -2) {
                Ugame.clearInfo();
                this.props.navigation.navigate('Login');
                return;
            }
            if (res.StatusCode == 1) {
                _this.setState({
                    page: 1,
                    total: res.Data.total,
                    totalpage: res.Data.totalpage,
                    lists: res.Data.rows,
                });
            }


        })

    }
    _onEndReached() {
        console.log("我是有底线的");
        if (this.state.page < this.state.totalpage) {
            this.getServerData(() => { });
        }

    }
    _separator() {
        return <View style={{ height: 0.5, backgroundColor: '#ededed' }} />;
    }
    render() {
        let { lists } = this.state;
        if (lists.length == 0) {
            return (
                <View></View>
            );
        }
        return (
            <View style={Styles.container}>
                <FlatList
                    refreshControl={
                        <RefreshControl
                            refreshing={this.state.refreshing}
                            onRefresh={this._onRefresh}
                        />
                    }
                    data={lists}
                    keyExtractor={(item, index) => index.toString()}
                    renderItem={this._createNewsItem}
                    onEndReached={this._onEndReached.bind(this)}
                    onEndReachedThreshold={0.1}
                    ItemSeparatorComponent={this._separator}
                />

            </View>
        )
    }



}
